<template>
  <y-dialog @close="emit('close')" width="1000px" append-to-body>
    <el-form
      ref="resolveBugRef"
      :model="form"
      :rules="rules"
      size="default"
      label-width="120px"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="解决方案" prop="extras.resolved_type">
            <y-select
              v-model="form.extras.resolved_type"
              placeholder="请选择解决方案"
              :options="tms_bug_resolved_type"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="解决版本" prop="extras.resolved_version">
            <y-select
              v-model="form.extras.resolved_version"
              placeholder="请选择解决版本"
              :optionsFunc="listVersion"
              :optionsParams="{ product: props.product, nopaginate: true }"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col>
          <el-form-item label="描述" prop="description">
            <y-editor
              editorId="editorBugResolved"
              v-model="form.description"
              placeholder="请输入描述信息"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="submit">确定</el-button>
        <el-button type="warning" @click="reset">重置</el-button>
        <el-button @click="emit('close')">取消</el-button>
      </div>
    </template>
  </y-dialog>
</template>

<script setup name="BugResolve">
import { addBugOperate } from "@/api/tms/bug/operate";
import { listVersion } from "@/api/pms/version";

const { proxy } = getCurrentInstance();

const { tms_bug_resolved_type } = proxy.useDict("tms_bug_resolved_type");

const emit = defineEmits(["submited", "close"]);

const data = reactive({
  form: {
    description: "",
    extras: {
      resolved_type: "",
      resolved_version: "",
    },
  },
  rules: {
    "extras.resolved_type": [
      { required: true, message: "解决方案不能为空", trigger: "blur" },
    ],
    "extras.resolved_version": [
      { required: true, message: "解决版本不能为空", trigger: "blur" },
    ],
    description: [
      { required: true, message: "解决描述不能为空", trigger: "blur" },
    ],
  },
});

const { form, rules } = toRefs(data);

const props = defineProps({
  id: { type: String, default: undefined },
  product: { type: String, default: undefined },
  getMethod: { type: Function, default: undefined },
  updateMethod: { type: Function, default: undefined },
});

/** 重置Form方法 */
function reset() {
  form.value = {
    description: "",
    extras: {
      resolved_type: "",
      resolved_version: "",
    },
  };
}

/** 提交按钮事件 */
const submit = async () => {
  try {
    await proxy.$refs["resolveBugRef"].validate();
    const bug = await props.getMethod(props.id);
    await props.updateMethod({ ...bug.data, status: "resolved" });
    await addBugOperate({
      bug: props.id,
      operation: "RESOLVE",
      description: form.value.description,
      extras: {
        resolved_type: form.value.extras.resolved_type,
        resolved_version: form.value.extras.resolved_version,
      },
    });
    proxy.$modal.msgSuccess("操作成功");
    submited();
  } catch (error) {
    console.error("操作失败:", error);
  }
};

/** submited事件 */
function submited() {
  emit("submited");
  emit("close");
}
</script>
